using System;
using System.Diagnostics;
using System.Globalization;
using ManagementInstrumentation = System.Management.Instrumentation.Instrumentation;
using System.Xml.XPath;

namespace TKH.Biztalk.Diagnostics
{
    /// <summary>
    /// 
    /// </summary>
    public class WMIEventListener: System.Diagnostics.TraceListener
    {
        
        /// <summary>
        /// 
        /// </summary>
        public WMIEventListener()
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.Close();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="eventCache"></param>
        /// <param name="source"></param>
        /// <param name="severity"></param>
        /// <param name="id"></param>
        /// <param name="data"></param>
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType severity, int id, params object[] data)
        {
            if (severity <= TraceEventType.Error)
            {
                foreach (object item in data)
                {
                    if (item is BiztalkEvent)
                    {
                        ManagementInstrumentation.Fire(item as BiztalkEvent);
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="eventCache"></param>
        /// <param name="source"></param>
        /// <param name="severity"></param>
        /// <param name="id"></param>
        /// <param name="data"></param>
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType severity, int id, object data)
        {
            if (severity <= TraceEventType.Error)
            {
                if (data is BiztalkEvent)
                {
                    ManagementInstrumentation.Fire(data as BiztalkEvent);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public override void Write(string message)
        {
            throw new NotSupportedException(string.Format("{0} does not support Write(string)", this.GetType().Name));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public override void WriteLine(string message)
        {
            throw new NotSupportedException(string.Format("{0} does not support WriteLine(string)", this.GetType().Name));
        }
    }
}
